<style lang="scss" scoped>
</style>

<template>
  <div style="display: flex; flex-direction: column; height: 100vh">
    <img src="@/bl-img/dl-head.jpg" width="100%" alt="" />
    <div style="flex: 1; padding: 20px">
      <van-field
        style="border-bottom: 1px solid #ccc"
        v-model="email"
        left-icon="envelop-o"
        placeholder="请输入邮箱"
      />
      <van-field
        style="border-bottom: 1px solid #ccc"
        v-model="pwd"
        left-icon="closed-eye"
        type="password"
        placeholder="请输入密码"
      />
      <p style="display: flex; justify-content: space-between; margin-top: 10px">
        <span style="color: #ccc; font-size: 12px" @click="router.push('/forgetone')">忘记密码</span>
        <span style="color: #2894FF; font-size: 14px" @click="router.push('/register')">申请入驻</span>
      </p>
      <el-button type="primary" round @click="submit" size="large" style="width: 90%; position: absolute; bottom: 50px">登录</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { JSEncrypt } from 'jsencrypt'
import { ElMessage } from 'element-plus'
import { request } from '../../axios/request'
import { useRouter } from 'vue-router'

const router = useRouter()

// 登录数据
const email = ref('')
const pwd = ref('')

// 登录
const submit = async() => {
  const crypt = new JSEncrypt()
  const publicKey =
    'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuQd3gESx7VdIyRYUWjmjg61VIgUK6F45hClmqUMZ7xNiT5rlLM6e78osMvBF/yP7cVm7pK+NMCDWoVS1/AETpxJYqUlIC77ZAU8/MnP96IupnJL87vqhPcpdv7+VqLM38ls++yuD/F/HSoOQTv/leJh+dgE/4EYAJjOWFAbYfXwIDAQAB'
  crypt.setPublicKey(publicKey)
  const newPwd = ref(crypt.encrypt(pwd.value))
  await request.post('/api/health/doctor/v1/login', {
    email: email.value as string,
    pwd: newPwd.value as string
  }, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }).then((res) => {
    console.log(res)
    let { message, status, result } = res.data
    if (status === '0000') {
      ElMessage.success(message)
      localStorage.setItem('result', JSON.stringify(result))
      localStorage.setItem('doctorId', JSON.stringify(result.doctorId))
      localStorage.setItem('sessionId', JSON.stringify(result.sessionId))
      router.push('/home')
    }else {
      ElMessage.error(message);
    }
  })
}
</script>